import { useState, useCallback } from 'react';
import { home } from '@/api';

export default function useHome() {
  //数据看板
  const [dataBoard, setDataBoard] = useState([]);
  const getDataBoard = useCallback(async (params: { type: string }) => {
    const res = await home.getDataBoard(params);
    if (res && res.data) {
      setDataBoard(res.data);
    } else {
      setDataBoard([]);
    }
  }, []);

  //数据看板饼图
  const [dataBoardPie, setDataBoardPie] = useState([]);
  const getDataBoardPie = useCallback(
    async (params: { industrial_chain_id: string }) => {
      const res = await home.getDataBoardPie(params);
      if (res && res.data) {
        setDataBoardPie(
          res.data.map((item: any) => {
            return {
              name: item.name,
              value: item.val || 0,
            };
          }),
        );
      } else {
        setDataBoardPie([]);
      }
    },
    [],
  );

  //企业名单
  const [entList, setEntList] = useState([]);
  const getEntList = useCallback(
    async (params: { industrial_chain_parent: string }) => {
      const res = await home.getEntList(params);
      if (res && res.data) {
        setEntList(res.data);
      } else {
        setEntList([]);
      }
    },
    [],
  );

  //分析报告

  const [reportList, setReportList] = useState([]);
  const getReportList = useCallback(async (params: { theme: string }) => {
    const res = await home.getReportList(params);
    if (res && res.data) {
      setReportList(res.data);
    } else {
      setReportList([]);
    }
  }, []);
  return {
    dataBoard,
    getDataBoard,
    dataBoardPie,
    getDataBoardPie,
    reportList,
    getReportList,
    entList,
    getEntList,
  };
}
